//
//  StatusViewModel.swift
//  JMWB
//
//  Created by 个人电脑 on 16/3/22.
//  Copyright © 2016年 个人电脑. All rights reserved.
//

import UIKit

class StatusViewModel {
    var status : Status?
    
    init(status : Status){
      self.status = status
        // 1.对来源进行处理
        //两个可选类型使用guard else进行校验,两个类型之间可以使用逗号隔开,如果是一个可选类型,一个判断语句,那么需要使用where隔开,在此where相当于OC中的&&,两个语句只要有其中一个不成立,那么就是不成立,将会执行else{}中的代码块
        // 1.判断来源是否有值
        if let source = status.source where source != ""{
       
        // 2.截取字符串
        // 2.1.获取截取的起始位置
        let startIndex = (source as NSString).rangeOfString(">").location + 1
        // 2.2.获取截取的长度
        let length = (source as NSString).rangeOfString("</").location - startIndex
        sourceText = "来自" + (source as NSString).substringWithRange(NSRange(location: startIndex, length: length))
        }
      // 2.会员等级
        let mbrank = status.user?.mbrank ?? 0
        if mbrank >= 1 && mbrank <= 6{
            // 1.拼接会员图片名称
            let vipImageName = "common_icon_membership_level\(mbrank)"
            // 2.设置会员图片
            vipImage = UIImage(named: vipImageName)
        }else{
            vipImage = nil
        }

      // 3.认证图标
        let type = status.user?.verified_type ?? -1
        switch type{
        case 0:
            verifiedImage = UIImage(named: "avatar_vip")
        case 2,3,5:
            verifiedImage = UIImage(named: "avatar_enterprise_vip")
        case 220:
            verifiedImage = UIImage(named: "avatar_grassroot")
        default:
            verifiedImage = nil
        }
        //4.处理头像的URL
        if let urlString = status.user?.profile_image_url{
        iconURL = NSURL(string: urlString)
        }
        // 5.处理微博所有的配图
        if let picURLDicts = status.pic_urls?.count != 0 ? status.pic_urls : status.retweeted_status?.pic_urls{
            for picURLDict in picURLDicts{
            let urlString = picURLDict["thumbnail_pic"] as? String
            let picURL = NSURL(string: urlString ?? "")
                picURLs.append(picURL!)
            }
        }
}
    //4.对微博数据进行的处理
    var createAtString : String?{
        guard let createAt = status?.created_at else{
            return ""
        }
        return NSDate.createTimeString(createAt)
    }

    //来源的显示字符串
    var sourceText : String?
    //认证显示的图标
    var verifiedImage : UIImage?
    //会员显示的图标
    var vipImage : UIImage?
    //头像的UR
    var iconURL : NSURL?
    //所有的配图对应的NSURL数组
    var picURLs : [NSURL] = [NSURL]()
}
